package com.example.hxds.dr.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.map.MapUtil;
import com.example.hxds.common.util.PageUtils;
import com.example.hxds.common.util.R;
import com.example.hxds.dr.controller.form.*;
import com.example.hxds.dr.service.DriverService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.HashMap;
import java.util.Map;


@RestController
@RequestMapping("/driver")
@Tag(name = "DriverController",description = "司机Web接口")
public class DriverController {

    @Autowired
    DriverService driverService;

    @PostMapping("/registerNewDriver")
    @Operation(summary = "新司机注册")
    public R registerNewDriver(@RequestBody @Valid RegisterNewDriverForm newDriverForm){
        String driverId = driverService.registerNewDriver(BeanUtil.beanToMap(newDriverForm));
        return R.ok().put("driverId",driverId);
    }

    @PostMapping("/updateDriverAuth")
    @Operation(summary = "更新司机实名认证信息")
    public R updateDriverAuth(@RequestBody @Valid UpdateDriverAuthForm form){
        Map<String, Object> param = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverAuth(param);
        return R.ok().put("rows", rows);
    }

    @PostMapping("/login")
    @Operation(summary = "司机登录")
    public R login(@RequestBody @Valid LoginForm form){
        HashMap map =driverService.login(form.getCode());
        return R.ok().put("result", map);
    }

    @PostMapping("/searchDriverBaseInfo")
    @Operation(summary = "查询司机基本信息")
    public R searchDriverBaseInfo(@Valid @RequestBody SearchDriverBaseInfoForm form){
        HashMap map = driverService.searchDriverBaseInfo(form.getDriverId());
        return R.ok().put("result",map);
    }

    @PostMapping("/searchDriverByPage")
    @Operation(summary = "查询司机分页记录")
    public R searchDriverByPage(SearchDriverByPageForm form){
        Integer page = form.getPage();
        Integer length = form.getLength();
        int start = (page - 1) * length;
        Map<String, Object> params = BeanUtil.beanToMap(form);
        params.put("start",start);
        PageUtils pageUtils = driverService.searchDriverByPage(params);
        return R.ok().put("result",pageUtils);
    }

    @PostMapping("/searchDriverRealSummary")
    @Operation(summary = "查询司机实名信息摘要")
    public R searchDriverRealSummary(@RequestBody @Valid SearchDriverRealSummaryForm form) {
        HashMap map = driverService.searchDriverRealSummary(form.getDriverId());
        return R.ok().put("result", map);
    }

    @PostMapping("/updateDriverRealAuth")
    @Operation(summary = "更新司机实名认证状态")
    public R updateDriverRealAuth(@RequestBody @Valid UpdateDriverRealAuthForm form) {
        Map param = BeanUtil.beanToMap(form);
        int rows = driverService.updateDriverRealAuth(param);
        return R.ok().put("rows", rows);
    }


}
